package org.zjt.rxjava.lession.second;

import io.reactivex.Flowable;
import io.reactivex.Observable;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description:  返回list 等集合，并支持背压式
 *
 * @author juntao.zhang
 * Date: 2018-06-20 下午6:19
 * @see
 */
@Slf4j
public class FlowableTest {

    /**
     * def 延迟（工厂延迟）
     * @param args
     */
    @Test
    public void defTest() {
        AtomicInteger atomicInteger = new AtomicInteger(10);
        Flowable<Integer> defer = Flowable.defer(() -> Flowable.range(0, atomicInteger.get()));
        atomicInteger.set(3);
        defer.map(String::valueOf).subscribe(log::info);
    }

    /**
     * 会抛出异常的创建
     */
    @Test
    public void unsafeCreateTest() {
        Flowable<String> flowable = Flowable.unsafeCreate(subscriber -> {
            subscriber.onNext("1111");
            subscriber.onNext("2222");
            subscriber.onComplete();
        });
        flowable.subscribe(log::info);
    }


}
